<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .modal-mask {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: none;
            z-index: 1000;
        }

        .modal {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
            display: none;
            z-index: 1001;
        }

        .modal-header {
            cursor: move;
        }
    </style>
</head>
<body>
    <button class="open-modal">打开模态框</button>
    <div class="modal-mask" style="display: none;">
        <div class="modal" style="display: none;">
            <div class="modal-header">
                <h3>模态框标题</h3>
            </div>
            <div class="modal-body">
                <p>模态框内容</p>
            </div>
            <div class="modal-footer">
                <button class="confirm-btn">确定</button>
                <button class="cancel-btn">取消</button>
            </div>
        </div>
    </div>

<script>
    // 获取相关元素
    const modal = document.querySelector('.modal');
    const modalMask = document.querySelector('.modal-mask');
    const closeBtn = document.querySelector('.close');
    const openBtn = document.querySelector('.open-modal');
    const modalHeader = document.querySelector('.modal-header');
    const confirmBtn = document.querySelector('.confirm-btn');
    const cancelBtn = document.querySelector('.cancel-btn');

    // 显示模态框
    function showModal() {
        modalMask.style.display = 'block';
        modal.style.display = 'block';
    }

    // 关闭模态框
    function closeModal() {
        modalMask.style.display = 'none';
        modal.style.display = 'none';
    }

    // 初始化拖拽状态
    function initDraggable() {
        let isDragging = false;
        let disX, disY;
        let initialX, initialY;

        modalHeader.addEventListener('mousedown', function(event) {
            isDragging = true;
            initialX = modal.offsetLeft;
            initialY = modal.offsetTop;
            disX = event.clientX - modal.offsetLeft;
            disY = event.clientY - modal.offsetTop;
        });

        document.addEventListener('mousemove', function(event) {
            if (!isDragging) return;
            const newX = Math.max(0, Math.min(event.clientX - disX, document.documentElement.clientWidth - modal.offsetWidth));
            const newY = Math.max(0, Math.min(event.clientY - disY, document.documentElement.clientHeight - modal.offsetHeight));
            modal.style.left = newX + 'px';
            modal.style.top = newY + 'px';
        });

        document.addEventListener('mouseup', function() {
            isDragging = false;
            modal.style.transform = 'translate(-50%, -50%)';
            modal.style.position = 'absolute';
            modal.style.top = `${newY}px`;
            modal.style.left = `${newX}px`;
        });
    }

    // 点击弹出层，显示模态框
    if (openBtn) {
        openBtn.addEventListener('click', showModal);
    }

    // 点击关闭按钮，关闭模态框
    if (closeBtn) {
        closeBtn.addEventListener('click', closeModal);
    }

    // 点击确定按钮，关闭模态框
    if (confirmBtn) {
        confirmBtn.addEventListener('click', closeModal);
    }

    // 点击取消按钮，关闭模态框
    if (cancelBtn) {
        cancelBtn.addEventListener('click', closeModal);
    }

    // 初始化拖拽功能
    if (modalHeader) {
        initDraggable();
    }
</script>

</body>
</html>